package cheme.ProcessEditor.Table;

import org.eclipse.draw2d.IFigure;

import cheme.ProcessEditor.ChemePresentation;
import cheme.ProcessEditor.Figures.AbstractResizableGroupFig;
import cheme.ProcessEditor.Parts.Streams.Tag.IDecoratorFigure;

public class TableFig extends AbstractResizableGroupFig implements ITable
{
	public TableFig()
	{	
		super();
		TableLayout layout = new TableLayout(this);
		setLayoutManager(layout);
	}
	
	protected void initPresentation()
	{
		super.initPresentation();
		
//		layout.setVertical(true);
//		layout.setStretchMinorAxis(true);
		
		setVisible(true);
		setOpaque(true);
		
		setPresentation(
				ChemePresentation.TableEditors.defaultBackground,
				ChemePresentation.TableEditors.defaultForeground,
				ChemePresentation.TableEditors.defaultBorder
		);
	}
	
	@Override
	public TableLayout getLayoutManager()
	{
		return (TableLayout)super.getLayoutManager();
	}
	
//	@Override
//	protected void paintFigure(Graphics graphics)
//	{
//		graphics.pushState();
//
//			setTransperant(graphics, ChemePresentation.TableEditors.Tags.opacity);
//			//fillGradient(graphics, ColorConstants.darkGray, ColorConstants.white);
//			
//			super.paintFigure(graphics);
//		
//		graphics.popState();
//	}
//	
//	private void setTransperant(Graphics graphics, int alpha)
//	{
//		graphics.setAlpha(alpha);	
//	}
	
//	private void fillGradient(Graphics graphics, Color top, Color bottom)
//	{
//		 graphics.setForegroundColor(top);
//		 graphics.setBackgroundColor(bottom);
//		 graphics.fillGradient(getBounds(), true);
//	}
	
	@Override
	public void add(IFigure figure, Object constraint, int index)
	{
		/*
		 * Give the newly added item its own layout
		 */
		IFigure unwrapped = figure;
		
		while(unwrapped instanceof IDecoratorFigure)
		{
			unwrapped = ((IDecoratorFigure)unwrapped).getContentPane();
		}
		
		getLayoutManager().add(unwrapped);
		
		super.add(figure, constraint, index);
	}
	
	@Override
	public void remove(IFigure figure)
	{
		IFigure unwrapped = figure;
		
		while(unwrapped instanceof IDecoratorFigure)
		{
			unwrapped = ((IDecoratorFigure)unwrapped).getContentPane();
		}
		
		getLayoutManager().remove(unwrapped);
		
		super.remove(figure);
	}
}
